/*
 * 文 件 名:  DispatchAction.java
 * 版    权:  Tydic Technologies Co., Ltd. Copyright 1993-2013,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  yanglong
 * 修改时间:  2013-11-10
 * 跟踪单号:  <需求跟踪单号>
 * 修改单号:  <需求修改单号>
 * 修改内容:  <修改内容>
 */
package com.hfut.jpoa.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hfut.jpoa.common.action.AbsAction;
import com.hfut.jpoa.common.dto.Pagination;
import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.dto.LogDTO;
import com.hfut.jpoa.model.Log;
import com.hfut.jpoa.model.User;
import com.hfut.jpoa.service.LogService;
import com.hfut.jpoa.service.UserService;
import com.hfut.jpoa.service.exception.DispatchException;
import com.hfut.jpoa.service.exception.LogException;
import com.hfut.jpoa.service.exception.UserException;

/**
 * <一句话功能简述> <功能详细描述>
 * 
 * @author yanglong
 * @version [1.0, 2013-11-10]
 * @since [产品/1.0]
 */
public class LogAction extends AbsAction<LogDTO>
{

	private static final Logger LOGGER = LoggerFactory
	        .getLogger(LogAction.class);

	private static final long serialVersionUID = -7819502638545092791L;

	private LogService logService;

	private Log log;

	private List<Log> lstLogs;

	/**
	 * 标志位：0-增加 1-修改 2-详情
	 */
	private String actionType;

	/**
	 * 包含分页接受前台变量
	 */
	private LogDTO logDTO;

	/**
	 * 分页结果集
	 */
	private ResultDto<Log> resultDto;

	/**
	 * 总页数
	 */
	private int totalPage;

	/**
	 * 创建者,以后从session中取
	 */
	private String creator = "yl";

	/**
	 * 部门员工
	 */
	private List<User> lstUsers;

	/**
	 * 用户服务
	 */
	private UserService userService;

	/**
	 * 加载列表页显示数据
	 */
	private void initDate() throws LogException
	{

		LogDTO dto = new LogDTO();
		Pagination pagination = new Pagination(0);
		dto.setPagination(pagination);

		resultDto = logService.queryInfoForPage(dto);
		totalPage = resultDto.getPagination().getTotalPage();
	}

	/**
	 * 加载调度中心分页列表
	 * 
	 * @return
	 * @throws CategoryException
	 */
	public String listInit() throws DispatchException
	{

		return "log_list_init";
	}

	/**
	 * 加载列表数据
	 * 
	 * @return
	 */
	public String list() throws LogException
	{

		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage");
		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (logDTO == null)
		{
			logDTO = new LogDTO();
		}
		logDTO.setPagination(pagination);
		resultDto = logService.queryInfoForPage(logDTO);

		// 封装用户信息，讲用户ID转成名称
		logService.packageUser(resultDto.getResult(), lstUsers);
		totalPage = resultDto.getPagination().getTotalPage();

		return "list";
	}

	/**
	 * 加载用户列表
	 * 
	 * @param user
	 * @return
	 * @throws DispatchException
	 */
	public List<User> initLstUser(User user) throws DispatchException
	{
		// 加载员工
		if (null == user)
		{
			user = new User();
		}
		List<User> lstUser;
		try
		{
			lstUser = userService.queryUser(user);
		} catch (UserException e)
		{
			LOGGER.error("加载员工异常：", e);
			throw new DispatchException("加载员工异常：", e);
		}
		return lstUser;
	}

	public String getActionType()
	{
		return actionType;
	}

	public void setActionType(String actionType)
	{
		this.actionType = actionType;
	}

	public int getTotalPage()
	{
		return totalPage;
	}

	public void setTotalPage(int totalPage)
	{
		this.totalPage = totalPage;
	}

	public UserService getUserService()
	{
		return userService;
	}

	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}

	public List<User> getLstUsers()
	{
		try
		{
			lstUsers = initLstUser(new User());
		} catch (DispatchException e)
		{
			LOGGER.error("加载部门员工异常：", e);
		}
		return lstUsers;
	}

	public void setLstUsers(List<User> lstUsers)
	{
		this.lstUsers = lstUsers;
	}

	public LogService getLogService()
	{
		return logService;
	}

	public void setLogService(LogService logService)
	{
		this.logService = logService;
	}

	public Log getLog()
	{
		return log;
	}

	public void setLog(Log log)
	{
		this.log = log;
	}

	public List<Log> getLstLogs()
	{
		return lstLogs;
	}

	public void setLstLogs(List<Log> lstLogs)
	{
		this.lstLogs = lstLogs;
	}

	public LogDTO getLogDTO()
	{
		return logDTO;
	}

	public void setLogDTO(LogDTO logDTO)
	{
		this.logDTO = logDTO;
	}

	public ResultDto<Log> getResultDto()
    {
    	return resultDto;
    }

	public void setResultDto(ResultDto<Log> resultDto)
    {
    	this.resultDto = resultDto;
    }

}
